package wqh.controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.Channel;
import wqh.domain.Device;
import wqh.domain.MapPos;
import wqh.pojo.MapRet;
import wqh.pojo.SelectIPandChannelCondition;
import wqh.service.ChannelService;
import wqh.service.DeviceService;
import wqh.service.MapPosService;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/map_pos")
public class MapPosController {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(MapPosController.class);

    @Autowired
    private MapPosService mapPosService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private ChannelService channelService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(MapPos mapPos)
    {
        try {
            if(mapPosService.findRecordByClass(mapPos).size() != 0){
                return MapRet.SQL_INSERT_ERROR();
            }
            mapPosService.insertRecord(mapPos);
            Device device = deviceService.findAllById(mapPos.getDevice_id());
            Channel channel = channelService.findByIPandChannel(new SelectIPandChannelCondition(device.getDevice_ip(),mapPos.getCh_x()));
            channel.setGeo_id(mapPos.getGeo_building_id());
            channelService.updateRecord(channel);
            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("添加通道数据地图坐标点异常：" + e);
            return MapRet.SQL_INSERT_ERROR();
        }
    }

    @RequestMapping(value = "/update1",method = RequestMethod.POST)
    public Object update1(@RequestBody  MapPos mapPos)
    {
        try {
            mapPosService.updateRecordByClass(mapPos);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("修改通道数据地图坐标点异常：" + e);
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Object delete(@RequestParam(value = "id",required = true) int id)
    {
        try {
            mapPosService.deleteById(id);
            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除通道数据地图坐标点异常：" + e);
            return MapRet.SQL_DELETE_ERROR();
        }
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll(@RequestParam(value = "geo_building_id",required = true) int geo_building_id)
    {
        try {
            List<MapPos> mapPosList = mapPosService.findRecordByGeoID(geo_building_id);
            if(mapPosList.size() == 0) return MapRet.SQL_ERROR();
            else return MapRet.DATA(mapPosList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取通道数据地图坐标点异常：" + e);
            return MapRet.SQL_ERROR();
        }
    }

}
